/**
 *  This code is a part of Quadratix project.
 * Copyright (C) 2009 - 2010 Valery A. Ilychev aka Sarutobi (ilychev at gmail dot com)
 * 
 *  This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package ru.sarutobi.quadratix;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class Calculator {

	private int size;
	private int[] board;

	/**
	 * Indexes for squares, that we can find in board. First array index is a
	 * square number, second - always 6
	 **/
	public final int[][][] indexes = { { { 1, 0 }, { 1, -1 }, { 0, -1 } },
			{ { 1, 1 }, { 2, 0 }, { 1, -1 } },
			{ { 0, -1 }, { -1, -1 }, { -1, 0 } },
			{ { 1, -1 }, { 0, -2 }, { -1, -1 } },
			{ { -1, 0 }, { -1, 1 }, { 0, 1 } },
			{ { -1, -1 }, { -2, 0 }, { -1, 1 } },
			{ { 0, 1 }, { 1, 1 }, { 1, 0 } }, { { -1, 1 }, { 0, 2 }, { 1, 1 } } };
	/**
	 * Таблица для быстрого поиска квадратов. Каждый квадрат представлен 5
	 * целыми числами - 4 координаты углов и стоимость квадрата.
	 */
	private Hashtable<Integer, List<int[]>> squares = new Hashtable<Integer, List<int[]>>();

	private static final Calculator instance = new Calculator();
	
	/** Default constructor */
	private Calculator(){
		
	}
	
	public static Calculator getCalculator(int size){
		instance.init(size);
		return instance;
	}
	
	public void init(int size) {
		int length = size * size;
		this.size = size;
		for (int i = 0; i < length; i++) {
			List<int[]> coords = new Vector<int[]>();
			squares.put(i, coords);

			int x = i % size;
			int y = i / size;
			for (int j = 1; j < size; j++)
				for (int q = 0; q < 8; q++) {

					int x1 = j * indexes[q][0][0] + x;
					int y1 = j * indexes[q][0][1] + y;

					int x2 = j * indexes[q][1][0] + x;
					int y2 = j * indexes[q][1][1] + y;

					int x3 = j * indexes[q][2][0] + x;
					int y3 = j * indexes[q][2][1] + y;
					if ((x1 >= 0) && (x1 < size) && (y1 >= 0) && (y1 < size)
							&& (x2 >= 0) && (x2 < size) && (y2 >= 0)
							&& (y2 < size) && (x3 >= 0) && (x3 < size)
							&& (y3 >= 0) && (y3 < size)) {
						int[] square = { i, x1 + size * y1, x2 + y2 * size,
								x3 + y3 * size, j };
						coords.add(square);
					}
				}
		}
	}

	public List<int[]> getSquares(int x, int y) {
		int pos = y * size + x;
		return squares.get(pos);
	}
	
	public List<int[]> getSquares(int pos){
		return squares.get(pos);
	}

	public void setBoard(int[] board) {
		this.board = board;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getMaxSquare(int x, int y) {
		int maxx = Math.max(x, size - x);
		int maxy = Math.max(y, size - y);
		return Math.min(maxx, maxy);
	}

	public int score(int x, int y) {
		return score(x + y * size);
	}

	public int score(int pos) {
		if  (board[pos] == Board.EMPTY) return 0;
		int score = 0;
		
		for (Iterator<int[]> i = squares.get(pos).iterator(); i.hasNext();){
			int[] square = i.next();
			if ((Board.EMPTY != board[square[0]]) &&
					(board[square[0]] == board[square[1]]) &&
					(board[square[0]] == board[square[2]]) &&
					(board[square[0]] == board[square[3]]))
				score += square[4];
		}
		return score;
	}
}
